Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
inquirer-autocomplete-prompt
Advanced tools
The inquirer-autocomplete-prompt npm package is an extension for the Inquirer.js library that provides an autocomplete prompt. It allows users to search and select from a list of options dynamically, which is particularly useful for large datasets or when the list of options is not known beforehand.
Basic Autocomplete Prompt
This feature allows users to search and select from a list of options dynamically. The code sample demonstrates a basic autocomplete prompt where users can type to filter through a list of fruits.
const inquirer = require('inquirer');
const autocompletePrompt = require('inquirer-autocomplete-prompt');
inquirer.registerPrompt('autocomplete', autocompletePrompt);
const questions = [
{
type: 'autocomplete',
name: 'fruit',
message: 'What is your favorite fruit?',
source: function(answersSoFar, input) {
input = input || '';
return new Promise(function(resolve) {
setTimeout(function() {
const fruits = ['Apple', 'Orange', 'Banana', 'Kiwi', 'Mango'];
const filtered = fruits.filter(fruit => fruit.toLowerCase().includes(input.toLowerCase()));
resolve(filtered);
}, 100);
});
}
}
];
inquirer.prompt(questions).then(answers => {
console.log('Your favorite fruit:', answers.fruit);
});
Dynamic Data Source
This feature allows the autocomplete prompt to fetch data from an external source dynamically. The code sample demonstrates how to use the GitHub API to search for repositories based on user input.
const inquirer = require('inquirer');
const autocompletePrompt = require('inquirer-autocomplete-prompt');
const axios = require('axios');
inquirer.registerPrompt('autocomplete', autocompletePrompt);
const questions = [
{
type: 'autocomplete',
name: 'repository',
message: 'Select a GitHub repository:',
source: async function(answersSoFar, input) {
input = input || '';
const response = await axios.get(`https://api.github.com/search/repositories?q=${input}`);
return response.data.items.map(repo => repo.full_name);
}
}
];
inquirer.prompt(questions).then(answers => {
console.log('Selected repository:', answers.repository);
});
Inquirer.js is a collection of common interactive command line user interfaces. It provides various types of prompts like input, confirm, list, rawlist, expand, checkbox, password, and editor. While it does not include an autocomplete prompt by default, it serves as the base library for inquirer-autocomplete-prompt.
Enquirer is a library for creating interactive CLI prompts. It offers a wide range of prompt types, including autocomplete, but is known for being more flexible and faster than Inquirer.js. Enquirer’s autocomplete prompt is similar to inquirer-autocomplete-prompt but is part of a more extensive set of features.
Prompt-sync is a synchronous prompt for node.js that is simple and easy to use. It does not support autocomplete functionality out of the box but can be extended with custom implementations. It is more lightweight compared to inquirer-autocomplete-prompt.
Autocomplete prompt for inquirer
npm install --save inquirer-autocomplete-prompt
This prompt is anonymous, meaning you can register this prompt with the type name you please:
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));
inquirer.prompt({
type: 'autocomplete',
...
})
Change autocomplete
to whatever you might prefer.
Note: allowed options written inside square brackets (
[]
) are optional. Others are required.
type
, name
, message
, source
[, default
, validate
, filter
, when
, pageSize
, prefix
, suffix
, askAnswered
, loop
, suggestOnly
, searchText
, emptyText
]
See inquirer readme for meaning of all except source, suggestOnly, searchText and emptyText.
source will be called with previous answers object and the current user input each time the user types, it must return a promise.
source will be called once at at first before the user types anything with undefined as the value. If a new search is triggered by user input it maintains the correct order, meaning that if the first call completes after the second starts, the results of the first call are never displayed.
suggestOnly is default false. Setting it to true turns the input into a normal text input. Meaning that pressing enter selects whatever value you currently have. And pressing tab autocompletes the currently selected value in the list. This way you can accept manual input instead of forcing a selection from the list.
validate is called with the entered text when suggestOnly is set to true. When suggestOnly is false, validate is called with the choice object. In addition it is called with the answers object so far.
searchText Is the text shown when searching. Defaults: Searching...
emptyText Is the text shown if the search returns no results. Defaults: No results...
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));
inquirer.prompt([{
type: 'autocomplete',
name: 'from',
message: 'Select a state to travel from',
source: function(answersSoFar, input) {
return myApi.searchStates(input);
}
}]).then(function(answers) {
//etc
});
See also example.js for a working example.
I recommend using this package with fuzzy if you want fuzzy search. Again, see the example for a demonstration of this.
ISC
FAQs
Autocomplete prompt for inquirer
We found that inquirer-autocomplete-prompt demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.